﻿using colorgetter;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EasyTools.UI
{
    public partial class FormSelect : Form
    {
        public FormSelect()
        {
            InitializeComponent();
            this.TransparencyKey = Color.Red;
            this.BackColor = Color.Red;
        }

        int status = 0;
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (status == 2 || status == 3)
            {
                e.Graphics.DrawRectangle(Pens.Blue, new Rectangle(start.X, start.Y, end.X - start.X + 1, end.Y - start.Y - 1));
            }
        }

        Point start = new Point();
        Point end = new Point();
        int ci = 0;

        private void FormSelect_MouseDown(object sender, MouseEventArgs e)
        {
            status = 1;
            start = e.Location;


        }

        private void FormSelect_MouseUp(object sender, MouseEventArgs e)
        {
            status = 3;

            end = e.Location;
            this.Refresh();
        }

        private void FormSelect_MouseMove(object sender, MouseEventArgs e)
        {
            if (status == 1 || status == 2)
            {
                end = e.Location;
                status = 2;
                this.Refresh();
            }
        }

        private void FormSelect_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)27)
            {
                this.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button2.Enabled = true;

            string folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string tmp = Path.Combine(folder, "gitmaker");
            if (Directory.Exists(tmp))
                Directory.Delete(tmp, true);
            Directory.CreateDirectory(tmp);
            ci = 0;

            timer1.Start();

            /*

            Size s = Win32Api.GetRealSize(new Size(end.X - start.X + 1, end.Y - start.Y - 1));
            Point p = Win32Api.GetRealPoint(this.Left + start.X, this.Top + start.Y);
            Bitmap b = new Bitmap( s.Width,s.Height);
            Graphics g = Graphics.FromImage(b);
            g.CopyFromScreen(p.X, p.Y, 0, 0, b.Size);
            


            string f = Path.Combine(tmp, "ffffff.bmp");
            b.Save(f);
            */

        }

        private void button2_Click(object sender, EventArgs e)
        {
            button1.Enabled = true;
            button2.Enabled = false;
            timer1.Stop();

            string folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string tmp = Path.Combine(folder, "gitmaker");
            string f = Path.Combine(tmp, "out.gif");

            using (FileStream fs = new FileStream(f, FileMode.Create))
            using (GifEncoder ge = new GifEncoder(fs))
            {
                for (int i = 0; i < ci; i++)
                {
                    ge.AddFrame(Image.FromFile(Path.Combine(tmp, i.ToString() + ".png")), 0, 0, new TimeSpan(0, 0, 0, 0, 100));
                }
               
            }
            if (textBox1.Text != "")
            {
                if (File.Exists(textBox1.Text))
                {
                    File.Delete(textBox1.Text);
                }
                File.Copy(f, textBox1.Text);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string tmp = Path.Combine(folder, "gitmaker");
            string f = Path.Combine(tmp, "out.gif");
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                File.Copy(f, saveFileDialog1.FileName);


        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Size s = Win32Api.GetRealSize(new Size(end.X - start.X + 1, end.Y - start.Y - 1));
            Point p = Win32Api.GetRealPoint(this.Left + start.X, this.Top + start.Y);
            if (s.Width <= 2 || s.Height <= 2)
            {
                button1.Enabled = true;
                button2.Enabled = false;
                timer1.Stop();
                MessageBox.Show("先选区域");
                
                return;
            }

            Bitmap b = new Bitmap(s.Width-2, s.Height-2);
            Graphics g = Graphics.FromImage(b);
            g.CopyFromScreen(p.X+2, p.Y+2, 0, 0, b.Size);

            string folder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string tmp = Path.Combine(folder, "gitmaker");
            string f = Path.Combine(tmp, ci.ToString() + ".png");
            b.Save(f, ImageFormat.Png);

            ci += 1;
            //b.Save("D:/test/ffffff.bmp");
        }

        private void FormSelect_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.None;     //设置窗体为无边框样式
            this.WindowState = FormWindowState.Maximized;    //最大化窗体 
            this.KeyPreview = true;
        }

        private void FormSelect_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }
    }
}
